package proxy;


import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class HelloInvocationHandler implements MethodInterceptor {


    private Hello target;

    public HelloInvocationHandler(Hello hello) {
        this.target = hello;
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        try {
            System.out.println("before!!!");
            // 调用被代理的方法
            return method.invoke(hello, args);
        } finally {
            System.out.println("after!!!");
        }
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        try {
            System.out.println("before!!!");
            // 调用被代理的方法
            return methodProxy.invokeSuper(o, objects);
        } finally {
            System.out.println("after!!!");
        }


    @Override
    public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
        System.out.println("before");
        return methodProxy.invoke(target, args);
    }

}
